//
//  FindViewController.m
//  BuffetNews
//
//  Created by 李荃荃 on 14-10-27.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "FindViewController.h"
#import "UIImageView+WebCache.h"
#import "WeiPlist.h"
#import "TableViewCell1.h"
#import "FindViewController2.h"

#import "VisionBeautiyController.h"

#import "NightModel.h"
#import "WebColor.h"
#import "Reachability.h"
#import <SystemConfiguration/SystemConfiguration.h>


@interface FindViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) UITableView * table;
@property (nonatomic, retain) NSMutableArray * arr;
@property (nonatomic, retain) NSMutableArray * arr1;
@property (nonatomic, retain) NSMutableArray * arrIamge;
@property (nonatomic, retain) NSMutableArray * arrIamge1;
@property (nonatomic, retain) NSMutableArray * sonsArr;
@property (nonatomic, retain) NSMutableArray * titleArr;
@property (nonatomic, retain) NSMutableArray * pkArr;
@property (nonatomic, retain) NSMutableArray * api_urlArr;

@end

@implementation FindViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.arrIamge1 = [ NSMutableArray array];
        self.sonsArr = [NSMutableArray array];
        self.pkArr = [NSMutableArray array];
        self.api_urlArr = [NSMutableArray array];
        self.titleArr = [NSMutableArray array];
    }
    return self;
}

- (void)dealloc
{
    [_titleArr release];
    [_pkArr release];
    [_api_urlArr release];
    [_table release];
    [_arr release];
    [_arr1 release];
    [_arrIamge release];
    [_arrIamge1 release];
    [_sonsArr release];
    [super dealloc];
}


//内容库
- (void)startTableView
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height - 110)];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.rowHeight = 55;
    self.table.alpha = 0.9;
    if ([NightModel shareInstance].open == 1) {
        self.table.backgroundColor = [WebColor dimGray];
    } else {
        self.table.backgroundColor = [UIColor whiteColor];
    }
    UIImageView *backgrnd=[[UIImageView alloc] initWithFrame:CGRectMake(0, -50, 320, 568)];
    backgrnd.contentMode=UIViewContentModeScaleAspectFit;
    
    backgrnd.image=[UIImage imageNamed:@"buff1.png"];
    [self.view addSubview:self.table];
    [self.view addSubview:backgrnd];
    [self.view bringSubviewToFront:self.table];
    [_table release];
    
}


- (void)viewWillAppear:(BOOL)animated
{
    [self.table reloadData];//  在视图出现的时候，刷新tableView
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
    titleLabel.text = @"内容库";
    titleLabel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = titleLabel;
    [self loadingData];
    [self startTableView];
    // Do any additional setup after loading the view.
}

//加载数据
- (void)loadingData
{

    WeiPlist *plist = [[WeiPlist alloc] init];

    NSDictionary *rootArray = [plist dictionaryData:@"apiBlockSorts"];
    
    
    self.arr = [NSMutableArray array];
    self.arr = [rootArray objectForKey:@"_root"];
    
    self.arr1 = [NSMutableArray array];
    self.arrIamge = [NSMutableArray array];
    for (NSDictionary *dic in self.arr) {
        [self.arr1 addObject:[dic objectForKey:@"block_title"]];
        [self.arrIamge addObject:[dic objectForKey:@"list_icon"]];
        [self.sonsArr addObject:[dic objectForKey:@"sons"]];
        [self.pkArr addObject:[dic objectForKey:@"pk"]];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"1"];
    if (!cell) {
        cell = [[[TableViewCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"1"]autorelease];
    }
    NSString * strImage = self.arrIamge[indexPath.row];
    [cell.iamge sd_setImageWithURL:[NSURL URLWithString:strImage]];
    cell.label1.text = [self.arr1 objectAtIndex:indexPath.row];
    cell.label1.font = [UIFont boldSystemFontOfSize:15];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if ([NightModel shareInstance].open == 1) {
        cell.backgroundColor = [WebColor dimGray];
        self.table.backgroundColor = [WebColor dimGray];
    } else {
        cell.backgroundColor = [UIColor whiteColor];
        self.table.backgroundColor = [UIColor whiteColor];
    }
    
    
    
    for (NSDictionary *dic in self.arr) {
        [self.arr1 addObject:[dic objectForKey:@"block_title"]];
        [self.arrIamge addObject:[dic objectForKey:@"list_icon"]];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.pkArr[indexPath.row] isEqualToString:@"44"] || [self.pkArr[indexPath.row] isEqualToString:@"68"] || [self.pkArr[indexPath.row] isEqualToString:@"121"]) {
        VisionBeautiyController *visionVC = [[VisionBeautiyController alloc] init];
        visionVC.sonsArr = self.sonsArr[indexPath.row];
        [self.navigationController pushViewController:visionVC animated:YES];
        [visionVC release];
    } else {
    
    
    FindViewController2 * findTow = [[FindViewController2 alloc] init];
    findTow.sons = self.sonsArr[indexPath.row];

    NSString *string = [self.arr[indexPath.row] objectForKey:@"pk"];
    findTow.block_title = string;     //  属性传值，用来判断视觉这一大类
    findTow.titleString = self.arr1[indexPath.row];

    [self.navigationController pushViewController:findTow animated:YES];
    }
    
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{


    //1. Setup the CATransform3D structure
    CATransform3D translation;
    // rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);
    translation = CATransform3DMakeTranslation(0, 480, 0);
    //rotation.m34 = 1.0/ -600;


    //2. Define the initial state (Before the animation)
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.layer.shadowOffset = CGSizeMake(10, 10);
    cell.alpha = 0;

    cell.layer.transform = translation;
    cell.layer.anchorPoint = CGPointMake(0, 0.5);

    //!!!FIX for issue #1 Cell position wrong------------
    if(cell.layer.position.x != 0){
        cell.layer.position = CGPointMake(0, cell.layer.position.y);
    }

    //4. Define the final state (After the animation) and commit the animation

    [UIView beginAnimations:@"translation" context:NULL];
    [UIView setAnimationDuration:0.8];
    cell.layer.transform = CATransform3DIdentity;

    cell.alpha = 1;
    cell.layer.shadowOffset = CGSizeMake(0, 0);

    [UIView commitAnimations];
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end
